// TCPSocketDlg.cpp

#include "stdafx.h"
#include "TCPSocketDef.h"
#include "TCPSocket.h"
#include "TCPSocketDlg.h"
#include "afxdialogex.h"
#include "TCPSocketClientDlg.h"
#include "TCPSocketServerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif//_DEBUG

BEGIN_MESSAGE_MAP(TCPSocketDlg, CDialogEx)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_SERVER, &TCPSocketDlg::OnBnClickedServer)
    ON_BN_CLICKED(IDC_CLIENT, &TCPSocketDlg::OnBnClickedClient)
    ON_BN_CLICKED(IDCANCEL, &TCPSocketDlg::OnBnClickedCancel)
END_MESSAGE_MAP()

TCPSocketDlg::TCPSocketDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(TCPSocketDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void TCPSocketDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}


BOOL TCPSocketDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    SetIcon(m_hIcon, TRUE);
    SetIcon(m_hIcon, FALSE);

    return TRUE;
}

void TCPSocketDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this);

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}

HCURSOR TCPSocketDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}

void TCPSocketDlg::OnBnClickedServer()
{
    if (!TCPSocketServerDlg::IsInitialize())
    {
        TCPSocketServerDlg::GetInstance()->Create(IDD_TCPSocket_Server, this);
        TCPSocketServerDlg::GetInstance()->ShowWindow(SW_SHOW);
        GetDlgItem(IDC_SERVER)->EnableWindow(FALSE);
        TCPSocketServerDlg::GetInstance()->GetDlgItem(IDC_CLOSE_SERVER)->EnableWindow(FALSE);

        CLOSE_SERVER_DIALOG_CALLBACK callback = std::bind(
            &TCPSocketDlg::OnCloseServerDialog, this);
        TCPSocketServerDlg::GetInstance()->SetCloseCallBack(callback);
    }
}

void TCPSocketDlg::OnBnClickedClient()
{
    if (!TCPSocketClientDlg::IsInitialize())
    {
        TCPSocketClientDlg::GetInstance()->Create(IDD_TCPSocket_Client, this);
        TCPSocketClientDlg::GetInstance()->ShowWindow(SW_SHOW);
        GetDlgItem(IDC_CLIENT)->EnableWindow(FALSE);
        TCPSocketClientDlg::GetInstance()->GetDlgItem(IDC_START_TRANSFER)->EnableWindow(FALSE);
        TCPSocketClientDlg::GetInstance()->GetDlgItem(IDC_CLOSE_CLIENT)->EnableWindow(FALSE);

        CLOSE_CLIENT_DIALOG_CALLBACK callback = std::bind(
            &TCPSocketDlg::OnCloseClientDialog, this);
        TCPSocketClientDlg::GetInstance()->SetCloseCallBack(callback);
    }
}


void TCPSocketDlg::OnBnClickedCancel()
{
    if (TCPSocketClientDlg::IsInitialize() || TCPSocketServerDlg::IsInitialize())
    {
        AfxMessageBox(L"Please close server/client dialog.");
    }
    else
    {
        CDialogEx::OnCancel();
    }
}

void TCPSocketDlg::OnCloseServerDialog()
{
    GetDlgItem(IDC_SERVER)->EnableWindow(TRUE);
}

void TCPSocketDlg::OnCloseClientDialog()
{
    GetDlgItem(IDC_CLIENT)->EnableWindow(TRUE);
}
